package com.xhj.bigfactory.bigFactory.GC_SoftReferance;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
/**
 * -
 *
 * @author 见见 pure_xhj@163.com @Date 2021-08-04 @Time 21:06
 */
public class SoftReferenceDemo {
  static class Data {
    int i;
  }

  public static void main(String[] args) {
    Data data1 = new Data();
    SoftReference<Data> data2 = new SoftReference<>(data1);
    System.out.println("data1 = " + data1);
    System.out.println("data2 = " + data2);

    System.out.println("=================================");

    data1 = null;
    System.out.println("data1 = " + data1);
    System.out.println("data2 = " + data2);

    System.out.println("=================================");

    System.gc();
    try {
      Thread.sleep(1234);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("data1 = " + data1);
    System.out.println("data2 = " + data2);

    System.out.println("===============OOM==================");
    for (int i = 0; i < 1024 * 1024 * 1024; i++) {
      ArrayList<Data> data = new ArrayList<>();
    }
    try {
      Thread.sleep(1234);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("data1 = " + data1);
    System.out.println("data2 = " + data2);
  }
}
